package com.zh.common.update

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.core.app.NotificationCompat
import com.blankj.utilcode.util.Utils

/**
 * 通知栏
 * https://blog.csdn.net/yechaoa/article/details/125465158
 */
class NotificationUtils {

    private var mManager: NotificationManager? = null
    private var mBuilder: NotificationCompat.Builder? = null
    private val NOTIFICATION_ID = 1
    private val CHANNEL_ID = "1"

    /**
     * 创建带进度条通知栏
     * @param context
     * @param resLogo
     */
    fun createNotificationForProgress(context: Context, resLogo: Int) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel =
                NotificationChannel(CHANNEL_ID, "1", NotificationManager.IMPORTANCE_DEFAULT)
            mManager?.createNotificationChannel(channel)
        }
        mBuilder = NotificationCompat.Builder(context, CHANNEL_ID)
            .setContentTitle("进度通知")
            .setContentText("下载中：0%")
            .setSmallIcon(resLogo)
            .setProgress(100, 0, false)
        mManager?.notify(NOTIFICATION_ID, mBuilder?.build())
    }

    /**
     * 更新通知栏进度
     * @param progress
     */
    fun updateNotificationForProgress(progress: Int) {
        // 1.更新进度
        mBuilder?.setContentText("下载中：$progress%")?.setProgress(100, progress, false)
        // 2.下载完成
        if (progress == 100) {
            mBuilder?.setContentText("下载完成！")?.setProgress(0, 0, false)
        }
        mManager?.notify(NOTIFICATION_ID, mBuilder?.build())
        if (progress == 100) {
            cancelNotification()
        }
    }

    /**
     * 创建普通通知栏
     */
    fun createNotificationForNormal(context: Context, resLogo: Int, clazz: Class<*>) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                CHANNEL_ID, "1", NotificationManager.IMPORTANCE_HIGH
            ).apply {
                description = "描述"
                setShowBadge(false) // 是否在桌面显示角标
            }
            mManager?.createNotificationChannel(channel)
        }
        // 点击意图
        val intent = Intent(context, clazz)
        val pendingIntent =
            PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
        // 构建配置
        mBuilder = NotificationCompat.Builder(context, CHANNEL_ID)
            .setContentTitle("普通通知") // 标题
            .setContentText("普通通知内容") // 文本
            .setSmallIcon(resLogo) // 小图标
//            .setLargeIcon() // 大图标
            .setPriority(NotificationCompat.PRIORITY_DEFAULT) // 7.0 设置优先级
            .setContentIntent(pendingIntent) // 跳转配置
            .setAutoCancel(true) // 是否自动消失（点击）
        // 发起通知
        mManager?.notify(NOTIFICATION_ID, mBuilder?.build())
    }

    fun cancelNotification() {
        mManager?.cancel(NOTIFICATION_ID)
    }

    companion object {
        val instance = NotificationUtils().apply {
            mManager =
                Utils.getApp().getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        }
    }
}